package plus.chendd.kafka.demo.producer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * 这是一个普通的消息生产者
 *
 * @author lihh
 */
public class HelloKafkaProducer {
  public static void main(String[] args) {
    Properties properties = new Properties();
    
    // 设置kafka 的服务器连接地址
    properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.56.12:9092");
    // 设置 String 的序列化
    properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    properties.put("value.serializer", StringSerializer.class);
    
    
    // 构建kafka对象
    KafkaProducer<String, String> producer = new KafkaProducer<>(properties);
    ProducerRecord<String, String> record;
    
    try {
      record = new ProducerRecord<>("order01", "name", "lihh");
      // 发送消息
      producer.send(record);
      System.out.println("message is send success.");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      producer.close();
    }
  }
}
